package cheng.rui.hong.frame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

import cheng.rui.hong.logic.CalcEnum;
import cheng.rui.hong.logic.Controller;

public class Frame extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextArea showArea;
	
	private MyButton addBtn;
	private MyButton minusBtn;
	private MyButton multiplyBtn;
	private MyButton divideBtn;
	private MyButton equalsBtn;
	
	private MyButton[] numBtn=new MyButton[12];
	
	private Controller controller;
	
	public Frame(String name){
		super(name);
		super.setBounds(200, 100, 450, 650);
		
		controller=new Controller(this);
		
		this.getContentPane().setLayout(new BorderLayout());
		this.showArea=new JTextArea(4,5);
		this.showArea.setAutoscrolls(false);
		this.showArea.setLineWrap(false);
		this.showArea.setEditable(false);
		this.getContentPane().add(new JScrollPane(showArea),BorderLayout.NORTH);
		
		
		JPanel dialPanel=new JPanel();
		GridLayout dialLyt=new GridLayout(4,3);
		
		dialPanel.setLayout(dialLyt);
		for(int i=1;i<10;i++){
			
			numBtn[i]=new MyButton(i+"");
			numBtn[i].addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent evt) {
					controller.pressNum(((MyButton)evt.getSource()).getText());
				}
			});
			dialPanel.add(numBtn[i]);
		}
		
		numBtn[0]=new MyButton(0+"");
		numBtn[0].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				controller.pressNum(((MyButton)evt.getSource()).getText());
			}
		});
		dialPanel.add(numBtn[0]);
		
		numBtn[10]=new MyButton(".");
		numBtn[10].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.dotPressed();
				
			}
		});
		dialPanel.add(numBtn[10]);
		numBtn[11]=new MyButton("c");
		numBtn[11].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.cancelPressed();
				
			}
		});
		dialPanel.add(numBtn[11]);
		
		this.getContentPane().add(dialPanel,BorderLayout.CENTER);
		
		
		JPanel calcPanel=new JPanel();
		GridLayout calcLayout=new GridLayout(5,1);
		calcPanel.setLayout(calcLayout);
		
		this.addBtn=new MyButton("+");

		this.addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.calculatePressed(CalcEnum.ADD);
			}
		});
		calcPanel.add(addBtn);
		
		this.minusBtn=new MyButton("-");

		this.minusBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.calculatePressed(CalcEnum.MINUS);
			}
		});
		calcPanel.add(minusBtn);
		
		this.multiplyBtn=new MyButton("*");

		this.multiplyBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.calculatePressed(CalcEnum.MULTIPLY);
			}
		});
		calcPanel.add(multiplyBtn);
		
		this.divideBtn=new MyButton("/");

		this.divideBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.calculatePressed(CalcEnum.DIVIDE);
			}
		});
		calcPanel.add(divideBtn);
		
		this.equalsBtn=new MyButton("=");
		this.equalsBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.equalsPressed();
			}
		});
		
		calcPanel.add(equalsBtn);
		
		this.getContentPane().add(calcPanel,BorderLayout.EAST);
		
		
		
		//this.validate();
		this.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		this.pack();
		this.setVisible(true);
		
		
	}
	
	public String getShowText(){
		return this.showArea.getText();
	}
	
	public void appShowText(String appStr){
		this.showArea.append(appStr);
	}
	
	public void removeStr(String remStr){
		String curText=this.showArea.getText();
		
		this.showArea.setText(curText.substring(0,curText.length()-remStr.length()));
		
	}
	
	public static void main(String[] args) {
		Frame frame=new Frame("Calculator");
		frame.setSize(600, 400);
		
	}
	
}
